<#--<#include "../layout/base.ftl">-->
<#--<#macro header>-->
<script type="text/javascript">
var companyList = [
<#if companyList??&&companyList?size&gt;0>
    {text: "", value: "0"},
    <#list companyList as company>
        {text: "${company[2]!""}", value: "${company[0]!""}"}<#if company_has_next>,</#if>
    </#list>
</#if>
];

$(function () {
    $("#im_search_panel").omPanel({
        title: "高级搜索"
    });


    var start = new Date();
    start.setFullYear(${startTime?string("yyyy")!""}, ${startTime?string("M")!""}-1,${startTime?string("dd")!""});
    var end = new Date();
    end.setFullYear(${endTime?string("yyyy")!""}, ${endTime?string("M")!""}-1,${endTime?string("dd")!""});

    $("#im_start_time").omCalendar({
        width: 100,
        maxDate: end,
        editable: false,
        onSelect: function (date, event) {
            $("#im_end_time").omCalendar({
                width: 100,
                minDate: date,
                editable: false
            });
        }
    });

    $("#im_end_time").omCalendar({
        width: 100,
        minDate: start,
        editable: false,
        onSelect: function (date, event) {
            $("#im_start_time").omCalendar({
                width: 100,
                maxDate: date,
                editable: false
            });
        }
    });

    $("#im_company_name").omCombo({
        width: 100,
        dataSource: companyList,
        listAutoWidth: true,
        filterStrategy: 'anywhere',
        editable: false,
        value: '0'
    });


    $("#im_status").omCombo({
        width: 100,
        dataSource: [
            {text: '未导入', value: '1'},
            {text: '导入久其成功', value: '7'},
            {text: '从久其删除成功', value: '9'},
            {text: '导入久其失败', value: '6'},
            {text: '从久其删除失败', value: '8'},
            {text: '导入U8成功', value: '3'},
            {text: '从U8删除成功', value: '5'},
            {text: '导入U8失败', value: '2'},
            {text: '从U8删除失败', value: '4'}
        ],
        value: '1',
        editable: false
    });

    $('span#im_button_search').omButton({
        width: 70,
        icons: {left: '${base}/css/default/images/advanced/search.png'},
        onClick: function (event) {
            $('#im_grid').omGrid({
                method: 'POST',
                dataSource: '${base}/invoice_merge/grid_json',
                extraData: {status: $("#im_status").omCombo('value'),
                    startTime: $("#im_start_time").val(),
                    endTime: $("#im_end_time").val(),
                    companyId: $("#im_company_name").omCombo('value'),
                    voucher: $("#im_voucher").val(),
                    operator: $("#im_operator").val()
                }
            });
        }
    });

    var grid = $('#im_grid').omGrid({
        height: 350,
        width: 'fit',
        autoFit: true,
        limit: 10,
        method: 'POST',
        dataSource: '${base}/invoice_merge/grid_json',
        extraData: {status: $("#im_status").omCombo('value'),
            startTime: $("#im_start_time").val(),
            endTime: $("#im_end_time").val(),
            companyId: $("#im_company_name").omCombo('value'),
            voucher: $("#im_voucher").val(),
            operator: $("#im_operator").val()
        },
        colModel: [
            {header: '公司名称', name: 'company', align: 'left'},
            {header: '合并日期', name: 'time', align: 'left'},
            {header: '合并单号', name: 'no', align: 'left', renderer: function (colValue, rowData, rowIndex) {
                return '<a href="#" onClick="imShowInviceDialog(' + colValue + ')">' + colValue + '</a>';
            }},
            {header: '会计区间', name: 'region', align: 'left'},
            {header: '凭证号', name: 'voucher', align: 'left'},
            {header: '合并人', name: 'operator', align: 'left'},
            {header: '操作', name: 'operation', align: 'center', renderer: function (colValue, rowData, rowIndex) {
                if (colValue == "add")
                    return '<button onClick="imShowDialog(' + rowData.id + ')">导入</button>';
                if (colValue == "delete")
                    return '<button onClick="imRevokeVoucher(' + rowData.id + ')">删除凭证</button>';
                if (colValue == "reset")
                    return '<button onClick="imRestVoucher(' + rowData.id + ')">重置</button>';
                return '';
            }}
        ]
    });

    $(window).scroll(function () {
        $("#im_grid").omGrid('resize');
    });

    $("#im_dialog").omDialog({
        title: "导入凭证",
        autoOpen: false,
        resizable: true,
        modal: true,
        width: 1000,
        buttons: [
            {
                text: "确定",
                click: function () {
                    var rows = $("#im_items tr");
                    var items = "[";
                    for (var i = 0; i < rows.length; i++) {
                        var inputs = $(rows[i]).find(":input");
                        items += "{";
                        for (var j = 0; j < inputs.length; j++) {
                            items += "\"" + $(inputs[j]).attr("name") + "\":\"" + $(inputs[j]).val() + "\",";
                        }
                        items = items.substring(0, items.length - 1) + "},";
                    }
                    items = items.substring(0, items.length - 1) + "]";
                    $.ajax({
                        type: "POST",
                        url: '${base}/invoice_merge/generate_voucher_json',
                        data: {'id': $("#im_id").val(), 'attachment': $("#im_attachment").val(),
                            importType: $("#im_importType").omCombo("value"), voucherNum: $("#im_voucherNumber").val(),
                            voucherItems: items},
                        dataType: 'json',
                        cache: false,
                        beforeSend: function () {
                            $("#im_dialog").omDialog("close");
                            $.blockUI({ message: '<h2>请稍后...</h2>' });
                        },
                        success: function (data) {
                            $.unblockUI();
                            $('span#im_button_search').omButton("click");
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }
                    });
                }
            },
            {
                text: "取消",
                click: function () {
                    $("#im_dialog").omDialog("close");
                }
            }
        ]
    });

    $("#im_invoice_dialog").omDialog({
        title: "合并凭证结算单明细",
        autoOpen: false,
        resizable: true,
        modal: true,
        width: 800,
        buttons: [
            {
                text: "关闭",
                click: function () {
                    $("#im_invoice_dialog").omDialog("close");
                }
            }
        ]
    });
});

var im_item_table_row = '<tr><td style="padding: 0px">' +
        '<input type="text" style="width:98%" name="digest" value=":digest"/></td>' +
        '<td align="left">:subjectname</td>' +
        '<td width="10%" align="right">:no</td>' +
        '<td width="10%" align="right">:debit</td>' +
        '<td width="10%" align="right">:credit</td>' +
        '<td width="10%">:departmentname</td>' +
        '<td width="10%">:product</td>' +
        '<td width="5%">:operation</td></tr>';

function imShowDialog(id) {
    var data = {"id": id};
    $("#im_id").val(id);
    $.post("${base}/invoice_merge/voucher_json", data, function (data, textStatus, jqXHR) {
        if (data.responseflag == "0") {
            $.omMessageBox.alert({
                type: "error",
                title: '提示',
                content: data.responsemessage
            });
            return;
        }
        $("#im_vchrtypecode").html(data.voucher.master.vchrtypecode);
        $("#im_createuser").html(data.voucher.master.createuser);
        $("#im_createdate").val(data.voucher.master.systemdate);
        $("#im_createdate").omCalendar({
            editable: false
        });
        $("#im_items").html("");
        if (data.voucher.items.length != 0) {
            var items_table = "";
            for (var i = 0; i < data.voucher.items.length; i++) {
                var item = data.voucher.items[i];
                var temp = im_item_table_row.replace(":digest", item.digest);
                temp = temp.replace(":subjectname", item.subjectname);
                if (item.isadd != 1)
                    temp = temp.replace(":no", "");
                else
                    temp = temp.replace(":no", '<input type="text" style="width:98%" name="no" value=""/>');

                if (item.isadd != 1)
                    temp = temp.replace(":debit", item.debit);
                else if (item.isadd == 1 && item.ismd == 1)
                    temp = temp.replace(":debit", '<input style="width:100px" type="text" name="debit" value="' + item.debit + '"/>');
                else if (item.isadd == 1 && item.ismc == 1)
                    temp = temp.replace(":debit", item.debit);

                if (item.isadd != 1)
                    temp = temp.replace(":credit", item.credit);
                else if (item.isadd == 1 && item.ismc == 1)
                    temp = temp.replace(":credit", '<input style="width:100px" type="text" name="credit" value="' + item.credit + '"/>');
                else if (item.isadd == 1 && item.ismd == 1)
                    temp = temp.replace(":credit", item.credit);

                if (item.isdept == 0)
                    temp = temp.replace(":departmentname", "");
                else
                    temp = temp.replace(":departmentname", '<select style="width:100px" name="departmentname"><option></option></select>');
                temp = temp.replace(":product", "");
//                    if (item.isproc == 0)
//                        temp = temp.replace(":product", "");
//                    else
//                        temp = temp.replace(":product", '<input type="text" style="width:100px" class="itext" value="请双击选择项目" readonly/>');

                if (item.isadd == 1)
                    temp = temp.replace(":operation", '<a href="#"  onclick="addRows(this,' + item.ismd + ')">添加</a>');
                else
                    temp = temp.replace(":operation", '');
                items_table += temp;
            }
            $("#im_items").html(items_table);
        }
        $("#im_importType").omCombo({
            width: 138,
            dataSource: [
                {text: '生成凭证号', value: '1'},
                {text: '指定凭证号', value: '2'}
            ],
            value: '1',
            editable: false
        });
        $("#im_attachment").val("");
        $("#im_dialog").omDialog('open');
    }, "json");
}

function imRevokeVoucher(id) {
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认撤销该凭证？',
        onClose: function (v) {
            if (v) {
                var data = {"id": id};
                $.post("${base}/invoice_merge/revoke_voucher_json", data,
                        function (data, textStatus, jqXHR) {
                            if (data.responseflag == "1") {
                                $('span#im_button_search').omButton("click");
                            }
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }, "json");
            }
        }
    });
}


function imRestVoucher(id) {
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认重置该凭证？',
        onClose: function (v) {
            if (v) {
                var data = {"id": id};
                $.post("${base}/invoice_merge/rest_voucher_json", data,
                        function (data, textStatus, jqXHR) {
                            if (data.responseflag == "1") {
                                $('span#im_button_search').omButton("click");
                            }
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }, "json");
            }
        }
    });
}
function addRows(row, ismd) {
    var temp = $(row).parent().parent().clone();
    $(temp).find("a").attr("onclick", "delRows(this)");
    $(temp).find("a").html("删除");
    if (ismd == 1) {
        $(temp).find("[name='debit']").val('');
    } else {
        $(temp).find("[name='credit']").val('');
    }
    $("#im_items tr:last").after(temp);
}

function delRows(row) {
    $(row).parent().parent().remove();
}

function imShowInviceDialog(id) {
    $.post("${base}/invoice_merge/invoice_merge_list", {id: id},
            function (data, textStatus, jqXHR) {
                $("#im_invoice_dialog").html(data);
                $("#im_invoice_dialog").omDialog("open");
            }, "html");
}
</script>
<#--</#macro>-->
<#--<#macro body>-->
<div id="im_search_panel" class="search_panel">
    <div>
        <span class="label">时&nbsp;&nbsp;&nbsp;间：</span>
        <input id="im_start_time" style="width: 80px" value="${startTime?string("yyyy-MM-dd")!""}"/>
        <span class="label">至</span>
        <input id="im_end_time" style="width: 80px" value="${endTime?string("yyyy-MM-dd")!""}"/>
        <span class="label">公&nbsp;&nbsp;&nbsp;司：</span>
        <input id="im_company_name" type="text"/>
        <span class="label">状&nbsp;&nbsp;&nbsp;态：</span>
        <input id="im_status"/>
        <span>凭证号：</span>
        <input id="im_voucher" type="text" class="input-text"/>
        <span>操作人：</span>
        <input id="im_operator" type="text" class="input-text"/>

        <span id="im_button_search">搜索</span>
    </div>
</div>
<br/>
<table id="im_grid"></table>

<div id="im_dialog">
    <form action='' method='post'>
        <input id="im_id" type="hidden"/>
        <table width="100%">
            <tr>
                <td width="70px">字号</td>
                <td><span id="im_vchrtypecode" style="font-size: 12px;font-weight: bold"></span></td>
                <td width="70px">制单人</td>
                <td id="im_createuser">
                </td>
                <td width="70px">制单日期</td>
                <td>
                    <input id="im_createdate" type="text" style="width: 120px"/>
                </td>
                <td width="70px">附单据数</td>
                <td>
                    <input id="im_attachment" type="text" style="width: 120px"/>
                </td>
            </tr>
        </table>
        <br/>
        <table width="100%" class="form outtable">
            <thead>
            <tr>
                <th>摘要</th>
                <th>科目</th>
                <th>发票号码</th>
                <th>借方</th>
                <th>贷方</th>
                <th>部门</th>
                <th>项目</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="im_items">
            </tbody>
        </table>
        <table width="100%">
            <tr>
                <td width="70px">导入方式</td>
                <td><input id="im_importType"/></td>
                <td width="70px">凭证号</td>
                <td><input id="im_voucherNumber" type="text" style="width: 118px"/></td>
            </tr>
        </table>
    </form>
</div>
<div id="im_invoice_dialog">
</div>
<#--</#macro>-->